﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TankQiandaoWx
{
    public class StringEncode
    {
        /// <summary>
        /// 將字符串編碼為16進制字符串
        /// </summary>
        /// <param name="inputStr">待編碼的字符串</param>
        /// <param name="encoding">字符編碼</param>
        /// <returns></returns>
        public static string Encode16HexStr(string inputStr, Encoding encoding)
        {
            byte[] tembyte = encoding.GetBytes(inputStr);
            StringBuilder sb = new StringBuilder();
            foreach (byte item in tembyte)
            {
                sb.AppendFormat("{0:X2}", item);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 將16進制字符串解密
        /// </summary>
        /// <param name="decryptString">待解密字符串</param>
        /// <param name="encoding">字符編碼</param>
        /// <returns></returns>
        public static string Decrypt16Hex(string decryptStr, Encoding encoding)
        {
            decryptStr = decryptStr.Replace(" ", "");
            decryptStr = decryptStr.Replace("\r\n", "");
            string txt16 = decryptStr;
            byte[] inputByteArray = new byte[txt16.Length / 2];
            for (int i = 0; i < inputByteArray.Length; i++)
            {
                inputByteArray[i] = Convert.ToByte(txt16.Substring(i * 2, 2), 16);
            }
            return encoding.GetString(inputByteArray);
        }
    }
}
